﻿using System.Linq;
using CI.Models.Data;
using CI.ViewModels.Diagramming;

namespace CI.ViewModels
{
    public abstract class InventoryItemSelectorViewModelBase : ItemSelectorViewModelBase
    {
        protected IQueryable<Item> PreFiltered { get; set; }

        protected InventoryItemSelectorViewModelBase(string groupName, DiagramViewModel parentViewModel) : base(groupName, parentViewModel)
        {

        }

        protected override void ApplyFilter()
        {
            var group = PreFiltered;

            group = ApplyFilter(group);

            Items.Clear();
            foreach (var i in group.OrderBy(g => g.TypeName)) Items.Add(i);
        }

        protected IQueryable<Item> ApplyFilter(IQueryable<Item> group)
        {
            if (!string.IsNullOrEmpty(FilterByName)) group = group.Where(g => g.TypeName.IndexOf(FilterByName) > -1);
            return group;
        }
    }
}